/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package framework.config;

import framework.db.SequenciaManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author du
 */
public class Database
{
    private static Database instance = null;

    private Connection conn = null;

    /**
     * getInstance
     *
     * @return DatabaseManager
     */
    public static Database getInstance() throws Exception
    {
        if ( instance == null )
        {
            instance = new Database();
        }
        
        instance.getConnection();
        return instance;
    }

    /**
     * DatabaseManager
     *
     */
    private Database()
    {
        try
        {
            Class.forName( "com.mysql.jdbc.Driver" );
        }

        catch( Exception e )
        {
            Utilities.handleException( e );
        }
    }

    /**
     * getConnection
     *
     * @return Connection
     * @throws Exception
     */
    public Connection getConnection() throws Exception
    {
        if ( conn == null || conn.isClosed() )
        {
            String url = PropertiesManager.getInstance().getString( "db.url", "" );
            String user = PropertiesManager.getInstance().getString( "db.user", "" );
            String passwd = PropertiesManager.getInstance().getString( "db.passwd", "" );
            
            conn = DriverManager.getConnection( 
                    String.format( "jdbc:mysql:%s?user=%s&password=%s",
                     url, user, passwd ) );
        }

        return conn;
    }
    
    /**
     * 
     * @param st
     * @return
     * @throws Exception 
     */
    public String fetchString( PreparedStatement st ) throws Exception
    {
        String result = "";
        ResultSet rs = st.executeQuery();
        
        if ( rs.next() )
        {
            result = rs.getString( 1 );
        }
        
        return result;
    }
    
    /**
     * 
     * @param st
     * @return
     * @throws Exception 
     */
    public int fetchInt( PreparedStatement st ) throws Exception
    {
        ResultSet rs = st.executeQuery();
        
        rs.next();
        
        return rs.getInt( 1 );
    }
    
    /**
     * 
     * @param tabela
     * @return
     * @throws Exception 
     */
    public int getNextId( String tabela ) throws Exception
    {
        return SequenciaManager.getInstance().getSequencia( tabela );
    }

    /**
     * 
     * @param sql
     * @return 
     */
    public PreparedStatement prepareStatement( String sql ) throws Exception
    {
        return conn.prepareStatement( sql );
    }

    /**
     * close
     *
     */
    public void close()
    {
        try
        {
            if ( !conn.isClosed() )
            {
                conn.close();
            }
        }

        catch( Exception e )
        {
            Utilities.handleException( e );
        }
    }
}
